package fem2.gui.actions;

import inf.vtk.pm.PipelineCollection;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import fem2.gui.ModelHolder;
import fem2.gui.ModelObserver;

abstract class DynamicAction extends ActionBase implements
		PropertyChangeListener, ModelObserver {

	private static final long serialVersionUID = 1L;

	DynamicAction(String name, PipelineCollection pc) {
		super(name, pc);
		// hack. needed because we have to receive events caused by the
		// model and chained by the data provider
		pc.addPropertyChangeListener(this);

		update();
	}

	/**
	 * Subclasses must implement this method to indicate if the action can be
	 * performed.
	 */
	abstract boolean isAvailable();

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (getCastedModel() != null) {
			getCastedModel().addObserver(this);
		}
		update();
	}

	void update() {
		setEnabled(isAvailable());
	}

	public void solutionChanged(ModelHolder mh) {
		update();
	}
}
